import requests
import pandas as pd
import os

xinfadi_url = "http://xinfadi.com.cn/getCat.html"
xinfadiHeaders = {  # Content-Length 不需要
    "Accept": "*/*",
    "Accept-Encoding": "gzip, deflate",
    "Accept-Language": "zh-CN,zh;q=0.9",
    "Cache-Control": "no-cache",
    "Connection": "keep-alive",
    "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
    "Host": "xinfadi.com.cn",
    "Origin": "http://xinfadi.com.cn",
    "Pragma": "no-cache",
    "Referer": "http://xinfadi.com.cn/index.html",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36",
    "X-Requested-With": "XMLHttpRequest"
}
data = {
    "prodCatid": "1186"
}
response = requests.post(xinfadi_url, headers=xinfadiHeaders, data=data)
response.encoding = response.apparent_encoding
# obj = json.loads(response.text)
# print(obj['list'][0]['prodName'])
obj2 = response.json()
# print(obj2)
# for item in obj2['list']:
#     print(item['prodName'])
#     print(item['avgPrice'])
#     print(item['highPrice'])
#     print(item['lowPrice'])

df = pd.DataFrame(obj2['list'])
df.to_csv('xinfadi.cvs', index=False, encoding='utf-8-sig')
